package com.omesoft.hksnore.user;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.omesoft.hksnore.R;
import com.omesoft.hksnore.util.CustomToast;
import com.omesoft.hksnore.util.dialog.MyProgressBarDialogUtil;
import com.omesoft.hksnore.util.json.ReErrorCode;
import com.omesoft.hksnore.util.json.SnorekillerJsonUtil;
import com.omesoft.hksnore.util.myactivity.MyActivity;
import com.omesoft.hksnore.util.titlebar.MyTitlebarUtil;
import com.omesoft.hksnore.util.web.CheckNetwork;
import com.omesoft.hksnore.util.web.DataCheckUtil;

/**
 * Created by Omesoft on 2017/2/17.
 */

public class SetPasswordActivity extends MyActivity implements View.OnClickListener {
    private EditText mPWD_et, mPWD_again_et;
    private Button mOK_btn;
    private String phoneNumber, verifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.snore_activity_set_password);
        initTitleBar();
        initView();
        initHandler();

    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        phoneNumber = bundle.getString("phone");
        verifyCode = bundle.getString("verifyCode");

        mPWD_et = (EditText) findViewById(R.id.set_psw_input);
        mPWD_again_et = (EditText) findViewById(R.id.set_psw_input_again);
        mOK_btn = (Button) findViewById(R.id.set_psw_ok);
        mOK_btn.setOnClickListener(this);
    }

    @Override
    protected void initTitleBar() {
        super.initTitleBar();
        MyTitlebarUtil.showTitleName(activity, R.string.set_password);
        MyTitlebarUtil.showIbLeft(activity,R.mipmap.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initHandler() {
        super.initHandler();
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case ReErrorCode.ERRCODE_SUCEED:
                        MyProgressBarDialogUtil.dismiss2Msg(R.string.set_password_register_succed);
                        postDelayed(new Runnable() {
                            @Override
                            public void run() {
                               jumpActivity2(context,LoginActivity.class);

                            }
                        },1500);
                        break;
                    case ReErrorCode.ERROR_REGISTER_FAIL:
                        MyProgressBarDialogUtil.dismiss();
                        CustomToast.showToasts(context, (String) msg.obj);
                        break;
                }
            }
        };
    }

    private boolean isCheckPWD() {
        String pwd = mPWD_et.getText().toString();
        String again = mPWD_again_et.getText().toString();
        if (pwd.length() == 0 || pwd.equals("")) {
            CustomToast.showToasts(context, R.string.set_password_error_empty);
            return false;
        }

        if (again.length() == 0 || again.equals("")) {
            CustomToast.showToasts(context, R.string.set_password_error_again_input_empty);
            return false;
        }

        if (!DataCheckUtil.checkMainData(pwd, 6, 30) || !DataCheckUtil.checkMainData(again, 6, 30)) {
            CustomToast.showToasts(context, R.string.set_password_error_combination);
            return false;
        }
        if (!again.equals(pwd)) {
            CustomToast.showToasts(context, R.string.set_password_error_no_identity);
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.set_psw_ok:
                if (CheckNetwork.checkNetwork(context)) {
                    if (isCheckPWD()) {
                        SnorekillerJsonUtil.Register(handler,activity, phoneNumber, verifyCode, mPWD_et.getText().toString(),2+"");
                    }
                }

                break;
        }
    }

}
